from ...service.buss_mis.device import DeviceService
from ...service.security_module import SecurityModule, FunctionName, PermissionName
# -*- coding: utf-8 -*-
'''
设备相关函数注册
'''


def register(jsonrpc, db_addr, db_port, db_name, db_user, db_pwd, inital_password, session):
    device_func = DeviceService(
        db_addr, db_port, db_name, db_user, db_pwd, inital_password, session)
    security_service = SecurityModule(
        db_addr, db_port, db_name, db_user, db_pwd, inital_password, session)

    @jsonrpc.method('IDeviceService.get_device_all')
    def __get_device(condition, page=None, count=None):
        permission_data = security_service.judge_permission_query(
            FunctionName.device_manage_all, PermissionName.query)
        res = device_func.get_device(permission_data, condition, page, count)
        return res

    @jsonrpc.method('IDeviceService.get_device_look')
    def __get_device(condition, page=None, count=None):
        permission_data = security_service.judge_permission_query(
            FunctionName.device_manage_look, PermissionName.query)
        res = device_func.get_device(permission_data, condition, page, count)
        return res

    @jsonrpc.method('IDeviceService.update_device')
    def __update_device(device):
        if 'id' in list(device.keys()):
            permission_data = security_service.judge_permission_other(
                FunctionName.device_manage_all, PermissionName.edit)
        else:
            permission_data = security_service.judge_permission_other(
                FunctionName.device_manage_all, PermissionName.add)
        res = device_func.update_device(device)
        return res

    @jsonrpc.method('IDeviceService.delete_device')
    def __delete_device(ids):
        permission_data = security_service.judge_permission_other(
            FunctionName.device_manage_all, PermissionName.delete)
        res = device_func.delete_device(ids)
        return res

    @jsonrpc.method('IDeviceService.get_device_log_all')
    def __get_device_log(condition, page=None, count=None):
        permission_data = security_service.judge_permission_query(
            FunctionName.device_manage_all, PermissionName.query)
        res = device_func.get_device_log(
            permission_data, condition, page, count)
        return res
